package com.xiaolei.MmkvKtx

import android.content.SharedPreferences
import androidx.fragment.app.Fragment


inline fun <reified T : Fragment> T.mmkvGet(key: String, defValue: Float): Float
{
    val mmkv = MMKVExt.getMMKV(this.requireContext())
    return mmkv.getFloat(key, defValue)
}

inline fun <reified T : Fragment> T.mmkvGet(key: String, defValue: Int): Int
{
    val mmkv = MMKVExt.getMMKV(this.requireContext())
    return mmkv.getInt(key, defValue)
}

inline fun <reified T : Fragment> T.mmkvGet(key: String, defValue: Long): Long
{
    val mmkv = MMKVExt.getMMKV(this.requireContext())
    return mmkv.getLong(key, defValue)
}

inline fun <reified T : Fragment> T.mmkvGet(key: String, defValue: Set<String>): Set<String>
{
    val mmkv = MMKVExt.getMMKV(this.requireContext())
    return mmkv.getStringSet(key, defValue)!!
}

inline fun <reified T : Fragment> T.mmkvGet(key: String, defValue: String): String
{
    val mmkv = MMKVExt.getMMKV(this.requireContext())
    return mmkv.getString(key, defValue)!!
}

/**
 * 对MMKV进行存值取值
 */
inline fun <reified T : Fragment> T.mmkvEdit(crossinline block: (SharedPreferences.Editor) -> Unit)
{
    val mmkv = MMKVExt.getMMKV(this.requireContext())
    val editor = mmkv.edit()
    block.invoke(editor)
    editor.apply()
    editor.commit()
}